package com.newtouch.bxzs.database.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.newtouch.bxzs.common.base.model.Organization;
import com.newtouch.bxzs.database.model.OrganizationBO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

/**
 * 组织机构Mapper类
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2020/12/21 14:47</td>
 * <td>linhongxi</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author linhongxi
 * @version 1.0.0
 * @since 1.0.0
 */
@Mapper
public interface OrganizationMapper extends BaseMapper<Organization> {

	/**
	 * 根据用户机构code查出用户的所有子级机构编码
	 *
	 * @param organizationCode 机构编码
	 * @return 当前机构及所有子级机构编码
	 */
	@Select(" SELECT getChildLst(#{organizationCode,jdbcType=VARCHAR,mode=INOUT}) ")
	String selectChildOrganizationCodeStr(@Param("organizationCode") String organizationCode);

	/**
	 * 根据用户机构CODE查出用户的所有上级机构CODE
	 *
	 * @param  organizationCode 机构编码
	 * @return 当前机构及所有上级机构编码
	 */
	@Select(" SELECT getParentLst(#{organizationCode,jdbcType=VARCHAR,mode=INOUT}) ")
	String selecParentOrganizationCodeStr(@Param("organizationCode") String organizationCode);
	/**
	 * 根据用户机构编码查询本级银行机构信息
	 *
	 * @param code 机构编码
	 * @return 机构信息
	 */
	@Select("SELECT a.ORGANIZATION_ID,a.ORGANIZATION_NAME,a.ORGANIZATION_CODE,a.ORGANIZATION_TELEPHONE,a.IS_ENABLE as enableFlag,a.CREATE_TIME, " +
			"CASE WHEN b.HAVE_CHAILD IS NOT NULL THEN " +
			"1 ELSE 0 " +
			"END AS HAVE_CHAILD FROM ( SELECT t.* FROM ASSISTANT_ORGANIZATION t where " +
			"t.ORGANIZATION_CODE = #{code, jdbcType=VARCHAR})a "+
			"LEFT JOIN ( SELECT DISTINCT t1.PARENT_ORGANIZATION_CODE HAVE_CHAILD FROM ASSISTANT_ORGANIZATION t1 ) b ON a.ORGANIZATION_CODE = b.HAVE_CHAILD " +
			"ORDER BY a.ORGANIZATION_CODE")
	List<OrganizationBO> organizationByUserCode(@Param("code") String code);

	/**
	 * 实时查询下级银行机构信息
	 *
	 * @param code 机构编码
	 * @return 下级机构信息
	 */
	@Select({ "<script>SELECT a.ORGANIZATION_ID,a.ORGANIZATION_NAME,a.ORGANIZATION_CODE,a.ORGANIZATION_TELEPHONE,a.IS_ENABLE as enableFlag,a.CREATE_TIME, " +
			"CASE WHEN b.HAVE_CHAILD IS NOT NULL THEN " +
			"1 ELSE 0 " +
			"END AS HAVE_CHAILD FROM ( SELECT t.* FROM ASSISTANT_ORGANIZATION t <where> " +
			"<choose> " +
			"<when test='code !=null and code != \"\"'> " +
			"AND t.PARENT_ORGANIZATION_CODE = #{code, jdbcType=VARCHAR} " +
			"</when> " +
			"<otherwise> " +
			"AND t.PARENT_ORGANIZATION_CODE is null " +
			"</otherwise> " +
			"</choose>" +
			"</where>) a LEFT JOIN ( SELECT DISTINCT t1.PARENT_ORGANIZATION_CODE HAVE_CHAILD FROM ASSISTANT_ORGANIZATION t1 ) b ON a.ORGANIZATION_CODE = b.HAVE_CHAILD " +
			"ORDER BY a.ORGANIZATION_CODE " +
			"</script>" })
	List<OrganizationBO> organizationByChild(@Param("code") String code);

	/**
	 * 根据机构主键查询机构信息
	 *
	 * @param organizationId 机构ID
	 * @return 机构信息
	 */
	@Select(value = "select a.*,ifnull(b.ORGANIZATION_NAME,'无') as parent_organization_name,ifnull(a.ORGANIZATION_DESCRIPTION,'无') as ORGANIZATION_DESCRIPTION_INFO \n" + "    from(\n"
			+ "            SELECT ASSISTANT_ORGANIZATION.*,ASSISTANT_ORGANIZATION.IS_ENABLE as enableFlag\n"
			+ "            from ASSISTANT_ORGANIZATION\n" + " WHERE ORGANIZATION_ID = #{organizationId}"
			+ "    )a\n" + "    left JOIN ASSISTANT_ORGANIZATION b\n"
			+ "    on a.PARENT_ORGANIZATION_CODE = b.ORGANIZATION_CODE")
	OrganizationBO organizationInfo(@Param("organizationId") String organizationId);

	/**
	 * 批量添加机构数据
	 * 
	 * @param organization
	 * @param createTime
	 * @return
	 */
	@Insert({ "<script>insert into ASSISTANT_ORGANIZATION(ORGANIZATION_ID,ORGANIZATION_CODE,ORGANIZATION_NAME,"
			+ "ORGANIZATION_TELEPHONE,ORGANIZATION_TYPE,ORGANIZATION_DESCRIPTION,PARENT_ORGANIZATION_CODE,"
			+ "IS_ENABLE,IS_DELETE,CREATE_USER_ID,CREATE_TIME,ORGANIZATION_NAME_FULL_SPELLING" + ") values"
			+ "<foreach collection='list' item='item' separator=',' >"
			+ "(REPLACE(uuid(),'-',''),#{item.organizationCode},"
			+ "#{item.organizationName},#{item.organizationTelephone},#{item.organizationType},"
			+ "#{item.organizationDescription},#{item.parentOrganizationCode}," + "1,0,'admin'," + "#{createTime}," +
			"#{item.organizationNameFullSpelling})"
			+ "</foreach>" + "ON DUPLICATE KEY UPDATE" + " UPDATE_USER_ID = values(CREATE_USER_ID)</script>" })
	int addOrganization(@Param("list") List<Organization> organization, @Param("createTime") Date createTime);
}
